package com.pluszg.nutrition.common.utils;


import com.pluszg.nutrition.common.exception.PlusBusinessException;
import com.pluszg.nutrition.common.exception.PlusException;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Optional;

import static lombok.AccessLevel.PRIVATE;

@NoArgsConstructor(access = PRIVATE)
public class Assert {

    public static void notBlank(String in) {
        if (StringUtils.isBlank(in)) {
            throw new PlusBusinessException();
        }
    }

    public static void notBlank(String in, String errorMsg) {
        if (StringUtils.isBlank(in)) {
            throw new PlusBusinessException(errorMsg);
        }
    }

    public static void notBlank(String in, int errorCode) {
        if (StringUtils.isBlank(in)) {
            throw new PlusException(errorCode);
        }
    }

    public static void notBlank(String in, String errorMsg, int errorCode) {
        if (StringUtils.isBlank(in)) {
            throw new PlusException(errorCode, errorMsg);
        }
    }

    public static void notBlank(String in, String errorMsg, Object result) {
        if (StringUtils.isBlank(in)) {
            throw new PlusBusinessException(errorMsg, result);
        }
    }

    public static void notNull(Object in) {
        Optional.ofNullable(in).orElseThrow(PlusBusinessException::new);
    }

    public static void notNull(Object in, int errorCode) {
        Optional.ofNullable(in).orElseThrow(() -> new PlusException(errorCode));
    }

    public static void notNull(Object in, String errorMsg) {
        Optional.ofNullable(in).orElseThrow(() -> new PlusBusinessException(errorMsg));
    }

    public static void notNull(Object in, String errorMsg, Object result) {
        Optional.ofNullable(in).orElseThrow(() -> new PlusBusinessException(errorMsg, result));
    }

    public static void notNull(Object in, String errorMsg, int errorCode) {
        Optional.ofNullable(in).orElseThrow(() -> new PlusException(errorCode, errorMsg));
    }

    public static void isTrue(Boolean condition) {
        if (condition == null || !condition) {
            throw new PlusBusinessException();
        }
    }

    public static void isTrue(Boolean condition, int errorCode) {
        if (condition == null || !condition) {
            throw new PlusException(errorCode);
        }
    }

    public static void isTrue(Boolean condition, String errorMsg) {
        if (condition == null || !condition) {
            throw new PlusBusinessException(errorMsg);
        }
    }

//    public static void isTrue(Boolean condition, String errorMsg, Object result) {
//        if (condition == null || !condition) {
//            throw new PlusBusinessException(errorMsg, result);
//        }
//    }

    public static void isTrue(Boolean condition, String errorMsg, int errorCode) {
        if (condition == null || !condition) {
            throw new PlusException(errorCode, errorMsg);
        }
    }

    public static void isNotTrue(Boolean condition) {
        if (condition != null && condition) {
            throw new PlusBusinessException();
        }
    }

    public static void isNotTrue(Boolean condition, String errorMsg) {
        if (condition != null && condition) {
            throw new PlusBusinessException(errorMsg);
        }
    }

    public static void isNotTrue(Boolean condition, int errorCode) {
        if (condition != null && condition) {
            throw new PlusException(errorCode);
        }
    }

    public static void isNotTrue(Boolean condition, String errorMsg, Object result) {
        if (condition != null && condition) {
            throw new PlusBusinessException(errorMsg, result);
        }
    }

    public static void isNotTrue(Boolean condition, String errorMsg, int errorCode) {
        if (condition != null && condition) {
            throw new PlusException(errorCode, errorMsg);
        }
    }

    /**
     * object array collection map
     *
     * @param object
     */
    public static void notEmpty(Object object) {
        if (ObjectUtils.isEmpty(object)) {
            throw new PlusBusinessException();
        }
    }

    /**
     * object array collection map
     *
     * @param object
     * @param errorCode
     */
    public static void notEmpty(Object object, int errorCode) {
        if (ObjectUtils.isEmpty(object)) {
            throw new PlusException(errorCode);
        }
    }

    /**
     * object array collection map
     *
     * @param object
     * @param errorMsg
     */
    public static void notEmpty(Object object, String errorMsg) {
        if (ObjectUtils.isEmpty(object)) {
            throw new PlusBusinessException(errorMsg);
        }
    }

    /**
     * object array collection map
     *
     * @param object
     * @param errorMsg
     * @param result
     */
    public static void notEmpty(Object object, String errorMsg, Object result) {
        if (ObjectUtils.isEmpty(object)) {
            throw new PlusBusinessException(errorMsg, result);
        }
    }

    /**
     * object array collection map
     *
     * @param object
     * @param errorMsg
     * @param errorCode
     */
    public static void notEmpty(Object object, String errorMsg, int errorCode) {
        if (ObjectUtils.isEmpty(object)) {
            throw new PlusException(errorCode, errorMsg);
        }
    }

    /**
     * 校验32位字符串id
     *
     * @param id
     * @param errorMsg
     */
    public static void idValidate(String id, String errorMsg) {
        notBlank(id, errorMsg);
        isTrue(id.length() == 32, errorMsg);
    }
}
